<?php

namespace app\com\simtoon\utils;
use app\com\simtoon\base\BaseCommand;
use yii\base\Exception;
use app\com\simtoon\log\LogUtils;
/**
 * Model辅助类
 */
class ModelUtils extends BaseCommand{
	

	/**
	 * 根据图册编号查詢图册表中的路径数据
	 * @param int $albumNo
	 * @return array 图册路径数组
	 */
	public function getAlbumPath($albumNo,$topic) {
        if($topic == 'zpt'){
            $sqlstr = "SELECT album_pno,album_path FROM `fang_album` WHERE album_pno = :album AND album_topic <> 'zpt'";
            $params = array(":album" => array($albumNo, "int"));
            return $this->queryAllByParams($sqlstr, $params);
        }
        //新房查询楼盘图
        if($topic=='lpt'){
            $sqlstr = "SELECT album_pno,album_path FROM `fang_new_album` WHERE album_pno = $albumNo
           AND fang_new_album.del_flag = 1
            AND album_type = 'xinfang'
            AND (album_topic = 'xgt' OR album_topic = 'qwt' OR album_topic = 'ybj' OR album_topic = 'hxt')";
            return $this->queryAll($sqlstr);
            /*  $params = array(":album" => array($albumNo, "int"));
              return $this->queryAllByParams($sqlstr, $params);*/

        }
        //惠居房源图片
        if($topic=='hj'){
            $sqlstr = "SELECT album_path,album_pid FROM huiju.rent_album where album_pid= $albumNo
            and album_type='rent'
            and deleted=0";
            return $this->queryAll($sqlstr);




        }

        //查询新房户型图   2015.11.26  yangliang
        if($topic=='layout_hxt' || $topic == 'xgt'){
            $topic = ($topic == 'layout_hxt')?'hxt':$topic;
            $sqlstr = "SELECT album_pno,album_path FROM `fang_new_album` WHERE album_pno = $albumNo
           AND fang_new_album.del_flag = 1
            AND album_type = 'xinfang'
            AND album_topic = '".$topic."'";
            return $this->queryAll($sqlstr);




        }

        if($topic=='xqt'){
            $sqlstr = "SELECT album_pno,album_path FROM `fang_album` WHERE album_pno = $albumNo
           AND fang_album.del_flag = 1
            AND album_type = 'domain'
            AND album_topic = 'xqt'";
            return $this->queryAll($sqlstr);
            /*  $params = array(":album" => array($albumNo, "int"));
              return $this->queryAllByParams($sqlstr, $params);*/

        }
        else if(is_array($albumNo)){
			$sqlstr = "SELECT album_pno,album_path FROM `fang_album` WHERE album_pno in(".implode(",", $albumNo).")";
			return $this->queryAll($sqlstr);		
		}else{
			$sqlstr = "SELECT album_pno,album_path FROM `fang_album` WHERE album_pno = :album AND album_topic = :topic";
			$params = array(":album" => array($albumNo, "int"),":topic" => array($topic, "string"));
			return $this->queryAllByParams($sqlstr, $params);
		}
	}


    /**
     * 根据户型父编号查询在售户型
     * @param $layout_pid
     */
    public function getLayout($layout_pid){
        if(!empty($layout_pid)){
            $sqlstr = "SELECT room_num FROM fang_layout WHERE layout_pid = ".$layout_pid." AND layout_status = 0";
            return $this->queryAll($sqlstr);
        }
    }


    /**
     * 根据户型父编号查询户型信息
     * @param $layout_pid
     * @return array
     */
    public function getLayoutInfo($layout_pid,$user_id){
        if(!empty($layout_pid)){
            $sqlstr = "SELECT fang_layout.layout_id,fang_layout.layout_pid,fang_layout.survey_no,fang_layout.room_num,fang_layout.hall_num,fang_layout.layout_fitment,
	                          fang_layout.kitchen_num,fang_layout.toilet_num,fang_layout.balcony_num,fang_layout.layout_area,fang_layout.layout_direct,
	                          fang_layout.layout_price,fang_layout.layout_label,fang_layout.layout_title,fang_layout.layout_status,fang_layout.layout_total,
                              (SELECT fang_new_album.album_path FROM fang_new_album WHERE fang_new_survey.album_pno = fang_new_album.album_pno AND fang_new_album.album_topic = 'hxt' LIMIT 1) AS thumb,fang_member_collection.layout_id AS layout_ch_no
                          FROM fang_layout
                      LEFT JOIN fang_new_survey ON fang_layout.survey_no = fang_new_survey.survey_no
                      LEFT JOIN fang_member_collection ON fang_layout.layout_id = fang_member_collection.layout_id AND fang_member_collection.user_id =".$user_id."
                          WHERE layout_pid =".$layout_pid;
            return $this->queryAll($sqlstr);
        }
    }

    /**
     * 截取utf-8字符串
     * 2015.9.7 yangliang
     * @param string $str 被截取的字符串
     * @param integer $start 起始位置
     * @param integer $length 截取长度(每个汉字为3字节)
     */
    public function utf8_strcut($str, $start, $length=null) {
        $len = strlen($str);
        $str = strip_tags($str);
        preg_match_all('/./us', $str, $match);
        $chars = is_null($length)? array_slice($match[0], $start ) : array_slice($match[0], $start, $length);

        unset($str);
        if($len > ($length*3)){
            return implode('', $chars).'...';
        }else{
            return implode('', $chars);
        }
    }

	/**
	 * 获取区域的名称
	 * @param int $cid
	 * @return 
	 */
	public function getAreaName($cid) {
		$sqlstr = "SELECT area_name  FROM fang_area WHERE area_cno = $cid";
        $res = $this->queryAll($sqlstr);
        if(is_array($res) && count($res) === 1)
        {
            return $res[0]['area_name'];
        }
        else
        {
            return $res;
        }
		return ;
	}

    /**
     *
     *
     * 获取城区 经纬度，
     */
	public function getAreas($cid){
        if(filter_var($cid,FILTER_VALIDATE_INT)){
            $sqlstr="select area_name,area_cno,longitude_x,latitude_y  from fang_area where area_pno= $cid";
            return $this->queryAll($sqlstr);
        }else{
            $arr=array();
            return $arr;
        }


    }



	/**
	 * 获取父区域的子区域id和name
	 *
	 */
	public function getChildArea($pid) {
        if(filter_var($pid, FILTER_VALIDATE_INT)){
            $sqlstr = "SELECT area_cno,area_name,area_pno FROM `fang_area` WHERE area_pno = $pid";
            return $this->queryAll($sqlstr);
        }else{
            $arr = array();
            return $arr;
        }
	}

    //查询首页搜素根据拼音查小区，
    public function getDomainName($keywords) {
        $sqlstr = "SELECT DISTINCT  domain_no,domain_name,(
		SELECT count(1) FROM `fang_house`, `fang_trade`	WHERE `fang_trade`.house_no = `fang_house`.house_no
          and `fang_house`.domain_no=`domain_py_index`.domain_no
		   AND `fang_trade`.trade_type = '100'
		   AND `fang_trade`.sale_state = '0'
				AND fang_house.house_application IN (0, 3)
	  ) saleNum
FROM `domain_py_index`  WHERE (domain_py_index.domain_qc LIKE  '".$keywords."%' OR domain_py_index.domain_szm LIKE '".$keywords."%' OR domain_py_index.domain_name LIKE  '%".$keywords."%')
    limit 0,8 ";
        return $this->queryAll($sqlstr);
    }

	public function getQueryCount($sql) {
		if(strlen($sql) > 0)
			$sql = "SELECT count(*) ".substr($sql,stripos($sql,"FROM"));
		$arr = $this->queryColumn($sql);
		if(count($arr)>0) return $arr[0];
	}

    /**
     * 查询字典表值
     * @param $type 字段名称
     * @param $value 字段值
     * @return string 字段label
     */
    public function getDict($type,$value) {
        $sqlstr = "SELECT dict_label FROM fang_dict WHERE dict_label_type = :dict_label_type AND dict_value = :dict_value";
        $params = array(":dict_label_type" => array($type, "string"), ":dict_value" => array($value, "string"));
        $res = $this->queryAllByParams($sqlstr, $params);
        if(is_array($res) && count($res) === 1)
        {
            return $res[0]['dict_label'];
        }
        else
        {
            return $res;
        }
    }


    /**
     * 2015-09-09 add by hetao: 保存短信验证码
     * @param $phone  电话号
     * @param $code  验证码
     * @param $c_time  创建时间
     * @param $e_time  过期时间
     * @return bool
     */
    public function save_sms_code($phone, $code, $c_time, $e_time){
        try{
            $phone = strtoupper(sha1(trim($phone)));
            $code = strtoupper(sha1(trim($code)));
            $sql = "UPDATE phone_code set code = '".$code."', create_time = ".$c_time.", expire_time = ".$e_time." WHERE phone = '".$phone."'";
            $num = $this->executeSql($sql);
            if($num === 0){
                $sql = "INSERT INTO phone_code VALUES ('".$phone."', '".$code."', ".$c_time.", ".$e_time.")";
                $num = $this->executeSql($sql);
                if($num === 0){
                    return false;
                }
            }
            return true;
        }catch (Exception $e){
            LogUtils::error($e->getMessage(), __METHOD__);
            return false;
        }
    }

    /**
     * 2015-09-09 add by hetao: 根据手机号查询验证码
     * @param $phone  电话号
     * @return array
     */
    public function get_sms_code($phone){
        try{
            $sql = "SELECT code, create_time, expire_time FROM phone_code WHERE phone = '".$phone."'";
            return $this->queryAll($sql);
        }catch (Exception $e){
            LogUtils::error($e->getMessage(), __METHOD__);
            return array();
        }
    }
    
    
    //根据看房团促销图编号，查询房源信息，
    public function getSeeHouse($cid) {
        $sqlstr = "select fang_see_line.domain_no,fang_see_line.see_path,fang_new_house.domain_name,
                           fang_new_house.average_price,fang_new_house.promotion_info,
                           fang_new_house.latest_open,fang_new_house.domain_address
                    from   fang_see_line,fang_new_house
                    where  fang_see_line.domain_no=fang_new_house.domain_no
                    and    fang_see_line.line_pno=$cid";
        return $this->queryAll($sqlstr);
    }
	
}

?>